<template>
    <div>


        <el-row :gutter="20">


            <el-col :span="8">
                <el-card>
                    <span style="font-weight: bold;font-size:25px">商品分类</span>
                    <el-row :gutter="10">
                        <el-col
                                style="margin:10px 20px"
                                :span="8"
                                v-for="(category,index) in categorys" :key="index">
                            <div @click="changeCategoryId(category.id,index)"
                                 class="item"
                                 :class="{ 'active': activeIndex === index }">
                                {{ category.name }}
                            </div>
                        </el-col>
                    </el-row>
                </el-card>
            </el-col>


            <el-col :span="16">
                <el-card>
                    <el-row :gutter="20" class="el-row">
                        <el-col :span="8" style="margin-bottom: 20px" v-for="(product,index) in products"
                                :key="index">
                            <el-card style="height: 220px">
                                <div @click="toProductInfo(product.id)">
                                    <div style="flex:1;text-align: center">
                                        <el-image style="width: 100px;height: 100px" fit="fit"
                                                  :src="product.image"></el-image>
                                    </div>
                                    <div style="flex:1;text-align: center;font-size: 11px;margin: 10px">{{
                                        product.name
                                        }}
                                    </div>
                                    <div style="flex:1;text-align: center;font-size: 15px;margin: 10px;color: #ff3e00">
                                        ￥{{
                                        product.price
                                        }}
                                    </div>
                                </div>

                            </el-card>
                        </el-col>
                    </el-row>


                    <el-pagination
                            style="float: right;margin-bottom: 10px"
                            background
                            layout="prev, pager, next"
                            :current-page.sync="currentPage"
                            @current-change="handleCurrentChange"
                            :total="total">
                    </el-pagination>
                </el-card>

            </el-col>
        </el-row>

    </div>
</template>

<script>
import {defineComponent} from 'vue'

export default defineComponent({
    name: "Category",
    data() {
        return {
            categorys: [],
            products: [
                {
                    id: 1,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 2,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 3,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                }, {
                    id: 1,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 2,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 3,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                }, {
                    id: 1,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 2,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 3,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                }, {
                    id: 1,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 2,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
                {
                    id: 3,
                    name: '华为 HUAWEI P60 Pro',
                    image: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    detail: '华为 HUAWEI P60 Pro',
                    detail_img: 'https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/mate40-white.png',
                    price: '2323',
                },
            ],
            categoryId: '1',
            activeIndex: 0,

            currentPage: 1,         // 当前页码
            pageSize: 12,            // 每页显示条数
            total: 12,               // 用户总数
        }
    },


    mounted() {
        this.loadAllCategory();
        this.selectByCategory();
    },


    methods: {


        toProductInfo(id) {
            this.$router.push('/productInfo?id=' + id)
        },

        // currentPage 改变时会触发
        handleCurrentChange(val) {
            // 获取  当前页 和 页面大小  currentPage、pageSize
            this.currentPage = val;

            // 发送请求获取相应数量的数据
            this.selectByCategory()
        },

        changeCategoryId(id, index) {
            this.categoryId = id;
            this.selectByCategory();
            this.activeIndex = index;
        },

        selectByCategory() {
            this.$request.post("/product/selectByCategory" + '?currentPage=' + this.currentPage
                + '&pageSize=' + this.pageSize
                + '&id=' + this.categoryId).then(res => {
                if (res.code === '200') {
                    this.products = res.data.list
                    this.total = res.data.total
                } else {
                    this.$message.error(res.msg)
                }
            })
        },

        // 加载所有的商品分类
        loadAllCategory() {
            this.$request.get('/category/loadAllCategory').then(res => {
                // console.log(res)
                if (res.code === '200') {
                    this.categorys = res.data;
                    // console.log(this.categorys)
                } else {
                    this.$message.error(res.msg);
                }
            })
        },
    }
})
</script>


<style scoped>

.el-row {
    margin-bottom: 20px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple-dark {
    background: #99a9bf;
}

.bg-purple {
    background: #d3dce6;
}

.bg-purple-light {
    background: #e5e9f2;
}

.grid-content {
    border-radius: 4px;
    min-height: 36px;
}

.row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
}


.item {
    padding: 10px;
    margin: 5px;
    cursor: pointer;
    font-size: 15px;
}

.item span {
    transition: text-shadow 0.3s ease; /* 添加过渡效果 */
}

.active {
    text-shadow: 0px 0px 5px rgba(0, 0, 0, 0.4); /* 添加文字阴影效果 */
}
</style>